// pages/my/personal-data/personal-data.js
const request = require('../../../../../utils/request')
const common = require('../../../../../utils/common.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        baseUrl: '',

        // headerImg:'',
        // nickName:'',
        // realName:'',
        showNickName: false,
        showRealName: false,
        showSex: false,
        sexList: [{
                name: '男',
                id: 1
            },
            {
                name: '女',
                id: 2
            }
        ],
        sex_text:'',
        sex:'',

        date: '',
        userInfo: {},
        upLoadimages: [],

        input_name: '',
        username:'',

        addressAll:'',


        xiaoquList:[],
        xiaoqu:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        this.getUserInfo()
        // let userInfo = wx.getStorageSync('userInfo') ? wx.getStorageSync('userInfo') : ''
        // this.setData({
           
        // })
        this.getXiaoquList()

    },
    
    getUserInfo() {
        let _this = this
        request._get('/api?method=user.info', {}).then(res => {
            _this.setData({
                userInfo: res.data,
                baseUrl: request.BASE_URL,
                input_name: res.data.nickname ?   res.data.nickname: '',
                sex:res.data.sex,
                sex_text:res.data.sex == 1 ?'男' :'女',
                date:res.data.birthday  ? res.data.birthday  :'',
                username:res.data.username,
                xiaoqu:res.data.xiaoqu,
                xiaoqu_name:res.data.xiaoqu_name,
                
            })
        })
    },
    inputWrite(e) {
        this.setData({
            [e.currentTarget.dataset.name]: e.detail.value
        })
    },

    choseImg() {
        let _this = this
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
            sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
            success: res => {
                let temFilePaths = res.tempFilePaths;
                wx.uploadFile({
                    url: _this.data.baseUrl + '/api?method=images.upload',
                    filePath: temFilePaths[0],
                    name: 'file',
                    success: function (res) {
                        let datas = JSON.parse(res.data)
                        _this.setData({
                            'userInfo.avatar': datas.data.url
                        })
                        wx.hideLoading()
                    }
                })
            }
        })
    },
    showDialog(e) {
        let type = e.currentTarget.dataset.type
        if (type == 1) {
            this.setData({
                showNickName: true
            })
        } else if (type == 2) {
            this.setData({
                showRealName: true
            })
        } else {
            this.setData({
                showSex: true
            })
        }
    },
    hideDialog() {
        this.setData({
            showNickName: false,
            showRealName: false,
            showSex:false
        })
    },
    sure(e) {
        let type = e.detail //1.昵称，2真实姓名
        if (type == 1) {
            if (!this.data.input_name) {
                getApp().toast('昵称不能为空')
                return
            }
            this.setData({
                'userInfo.nickname': this.data.input_name
            })
        } else if (type == 2) {
            if (!this.data.username) {
                getApp().toast('真实姓名不能为空')
                return
            }
            this.setData({
                'userInfo.username': this.data.username
            })
        }
        this.hideDialog()
    },
    bindDateChange: function (e) {
        this.setData({
            date: e.detail.value
        })
    },
    choseSex(e) {
        let item = e.currentTarget.dataset.item
        this.setData({
            sex_text: item.name,
            sex: item.id
        })
        this.hideDialog()
    },
    // 更换用户头像/api?method=user.changeavatar
    changeHeader() {
        request._get('/api?method=user.changeavatar',{avatar:this.data.userInfo.avatar}).then(res=>{

        })
    },
    submit(){
        this.changeHeader()
      
        request._get('/api?method=user.editinfo',{
            sex:this.data.sex,
            birthday:this.data.date,
            nickname:this.data.userInfo.nickname,
            username:this.data.username ? this.data.username :'',
            xiaoqu:this.data.xiaoqu
        }).then(res=>{
            getApp().toast(res.msg)
            if(this.data.xiaoqu && this.data.xiaoqu != wx.getStorageSync('localXiaoqu')) {
                getApp().globalData.changePoint  = true
                this.getUserInfo_change()
             }
            setTimeout(function() {
                wx.navigateBack({
                    delta: 1, // 回退前 delta(默认为1) 页面
                })
            })
            
        })
    },
     // 获取小区
     getXiaoquList() {
        let _this = this
        if(wx.getStorageSync('xiaoquList')) {
            this.setData({
                xiaoquList:wx.getStorageSync('xiaoquList')
            })
            return
        }
        request._post('/api?method=user.xiaoqu', {}, '', true).then(res => {
            this.setData({
                xiaoquList: res.data
            })
            wx.setStorage({
                key: 'xiaoquList',
                data:res.data
            })
        })
    },
    bindPickerChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            xiaoquIndex: e.detail.value,
            xiaoqu_name:this.data.xiaoquList[e.detail.value].name,
            xiaoqu:this.data.xiaoquList[e.detail.value].id,
        })
    },
    getUserInfo_change() {
        let _this = this
        request._get('/api?method=user.info', {},'noload').then(res => {
            let obj = {
                "id": res.data.xiaoqu,
                "name": res.data.xiaoqu_name,
                "lng":res.data.lng,
                "lat": res.data.lat,
                "image": "",
                "distance": res.data.distance,
            }
            wx.setStorage({
                key: 'localXiaoqu',
                data:obj
            })
        })
    },


})